package kaa.archivator;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {

    public static void main(String[] args) {
        Path outFile;
        Path myPath;

        if (args.length > 0) {
            if (args.length > 2 && args[0].startsWith("-a") && args[1].startsWith("-fn=")) {
                outFile = FileSystems.getDefault().getPath(args[1].split("-fn=")[1]);
                if(!Files.exists(outFile)){
//                Files.deleteIfExists(outFile);
                FileMetaDataRW rw = new FileMetaDataRW(outFile);
                for (int i = 2; i < args.length; ++i) {
                    myPath = FileSystems.getDefault().getPath(args[i]);
                    try {
                        rw.writeToFile(myPath);
                    } catch (IOException e) {
                        System.out.println("Can`t make archive\nmessage:\t" + e.getMessage());
                    }
                }
                }else{
                    System.out.println("delete file "+outFile);
                }
            } else if (args[0].startsWith("-r")) {
                myPath = FileSystems.getDefault().getPath(args[1]);
                try {
                    new FileMetaDataRW(myPath).readFromFile(myPath, args[2]);
                } catch (IOException e) {
                    System.out.println("Can`t restore archive\nmessage:\t" + e.getMessage());
                }
            } else if (args[0].startsWith("-s")) {
                myPath = FileSystems.getDefault().getPath(args[1]);
                try {
                    new FileMetaDataRW(myPath).readFromFile(myPath);
                } catch (IOException e) {
                    System.out.println("Can`t restore archive\nmessage:\t" + e.getMessage());
                }
            }
        }else{
            System.out.println("Usage:");
            System.out.println("-s 'archive name' : show archive contents");
            System.out.println("-r 'archive name' 'destination folder': extract archive contents to folder");
            System.out.println("-a -fn='archive name' 'file or folder' : archive files or folder");
            System.out.println("Note: It is important when adding directories to archive" +
                    " not to put slash or backslash at the end of the path but when extracting to folder they should be");
            System.out.println("examples : \n\t-s arch ;\n" +
                    "\t-r arch recovered/ ;\n" +
                    "\t-a -fn=arch test/input.txt test/file ;");
        }
    }
}

//-s arch
//-r arch recovered/
//-a -fn=arch test/input.txt test/file